import {
	defineStore
} from 'pinia';
import {
	getToken,
	setToken
} from '../utils/auth';
import {
	toast
} from '../utils/common';
import {
	login
} from '/api/login.js'
import config from '../config/config.js'


export const useCounterStore = defineStore('index', {
	state: () => {
		return {
			isLoginSuccess: false,
			filePath: config.baseUrl,
			userInfo: {},
		};
	},
	

	actions: {
		// 登录
		loging(obj) {
			console.log(obj);
			return new Promise((resolve, reject) => {
				login(obj).then(res => {
					// console.log();
					this.userInfo = res.data.member;
					setToken(res.data.access_token);
					this.isLoginSuccess = true;
					resolve()
				}).catch(err => {
					this.isLogin = false;
					reject(err)
				})
			})
		},
		// 注销
		logout() {},
		// 获取定位  位置信息
		getNowAddress() {
			let that = this
			return new Promise((res, rej) => {
				//位置信息默认数据
				uni.getLocation({
					type: "gcj02",
					success(res) {
						that.nowAddress.longitude = res.longitude;
						that.nowAddress.latitude = res.latitude;
						// 腾讯地图Api
						let qqMapsdk = new QQMapWX({
							key: '' //这里填写自己申请的key
						});
						qqMapsdk.reverseGeocoder({
							location,
							success(response) {
								let info = response.result;
								that.nowAddress.province = info.address_component
									.province;
								that.nowAddress.city = info.address_component.city;
								that.nowAddress.area = info.address_component
									.district;
								that.nowAddress.street = info.address_component
									.street;
								that.nowAddress.address = info.address;
								uni.setStorageSync('address', info.address)
								// toast(that.nowAddress.address)
							},
							fail(err) {
								toast('定位失败')
							}
						});
					},
					fail(err) {
						toast('定位失败')
					},
				});
			})
		}
	},
});